package cn.wh.org.controller;

import cn.wh.basic.util.AjaxResult;
import cn.wh.basic.util.PageList;
import cn.wh.org.domain.Details;
import cn.wh.org.query.DetailsQuery;
import cn.wh.org.service.IDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/det")
public class DetailsController {
    @Autowired
    private IDetailsService iDetailsService;

    @GetMapping("/{id}")

    public Details queryById(@PathVariable Long id){
        return iDetailsService.queryById(id);
    }

    @GetMapping
    public List<Details> queryAll(){
        return iDetailsService.queryAll();
    }

    @PostMapping
    public PageList<Details> queryPage(@RequestBody DetailsQuery query){
        return iDetailsService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Details details){
        try {
            if (details.getId() == null){
                iDetailsService.save(details);
            }else{
                iDetailsService.update(details);
            }
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("弄给和hiGV橘郡");

        }

    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id){
        try {
            iDetailsService.removeById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("弄给和hiGV橘郡");
        }
    }

    @DeleteMapping
    public AjaxResult removeAll(@RequestBody List<Long> ids){
        try {
            iDetailsService.deleteAll(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("弄给和hiGV橘郡");
        }
    }

    @PostMapping("/type")
    public PageList<Details> queryPageByType_id(@RequestBody DetailsQuery query){
        return iDetailsService.queryPageByType_id(query);
    }

}
